<?php
    class M_Book extends CI_Model {
        function __construct() {
            parent::__construct();
        }
        
        function GetNewOneBook() {
            $query = $this->db->query('select * from book where `Deleted`=0 order by Datetime DESC limit 0, 1');
            if ($query->num_rows() > 0) {
                $row = $query->row();
                return $row;
            } else {
                return NULL;
            }
        }
        function GetCategoryList() {
            $query = $this->db->query('select * from category where `Deleted`=0 order by STT ASC');
            $rows = array();
            
            if ($query->num_rows() > 0) {
                foreach ($query->result() as $row) {
                    $rows[] = $row;
                }
                return $rows;
            } else {
                return null;
            }
        }
        
        function GetReViewPage($url) {
            $query = $this->db->query("select * from category where `Deleted`=0 and url = '$url'");
            if ($query->num_rows() > 0) {
                $row = $query->row();
                return $row;
            } else {
                return NULL;
            }
        }
        
        public function Get10Books() {
            $query = $this->db->query('select b.Image bImage, b.ID bID, BookName, b.url bURL, b.Description bDescription, a.ID aID, AuthorName, b.Datetime bDatetime, a.url aURL   from book b, author a where b.author_ID = a.ID and b.`Deleted` = 0 order by Datetime DESC limit 0, 10');
            $rows = array();
            
            if ($query->num_rows() > 0) {
                foreach ($query->result() as $row) {
                    $rows[] = $row;
                }
                return $rows;
            } else {
                return null;
            }
        }
        
        function GetCategoryID($url) {
              $query = $this->db->query("select * from category where `Deleted` = 0 and url = '$url'");
            
            if ($query->num_rows() > 0) {
                $row = $query->row();
                return $row->ID;
            } else {
                return null;
            }  
        }
        
        function GetBooks ($url) {
            $cate_id = $this->GetCategoryID($url);
            $query = $this->db->query("select b.Image bImage, b.ID bID, BookName, b.url bURL, b.Description bDescription, a.ID aID, AuthorName, b.Datetime bDatetime, a.url aURL   from book b, author a where b.author_ID = a.ID and b.`Deleted` = 0 and category_ID = '$cate_id' order by Datetime DESC limit 0, 10");
            
            $rows = array();
            
            if ($query->num_rows() > 0) {
                foreach ($query->result() as $row) {
                    $rows[] = $row;
                }
                return $rows;
            } else {
                return null;
            }
        }
        
        public function GetListChapter() {
            
        }
        
    }
?>